<?php
namespace pgc;
use \hc\tpl;
use \hc\session;
use \hc\js;
class User extends tpl{
	public function __construct(){
		parent::__construct();
		session::init();
		if(isset($_SESSION['pgcinfo'])){
				$this->pgcinfo=$_SESSION['pgcinfo'];
		}else{
				$url=MODEL_URL."/Login/index.html";
			 echo 	js::to_url($url);exit;
		}
    $this->user_m=new \model\user_m;
		$this->role_m=new \model\role_m;
	}
	//用户管理
	public function index(){
		//超级管理员不显示
		$where=array();
		//$where['A.id']=array('!=',1);
    $data=$this->user_m->s_all_ajaxs($where);
		if(isset($_POST['ajaxs'])){
				ajax_return(true,'',$data);
		}
    $this->assign('listarr',$data['list']);
    $this->assign('pageshow',$data['pageshow']);
		$this->display('user/lists.tpl');
	}
	//添加用户
	public function userAdd(){
			if(isset($_POST['ajaxs'])){
				$data=ajaxs_from($_POST['arr']);
				if(trim($data['loginname'])==''){
						ajax_return(false,'登陆名不能为空');
				}
				if(trim($data['name'])==''){
						ajax_return(false,'用户名不能为空');
				}
				if(trim($data['pwd'])==''){
						ajax_return(false,'密码不能为空');
				}
				if(trim($data['face'])==''){
						ajax_return(false,'必须上传头像');
				}

				//判断唯一性
				$where_unique['loginname']=array('=',$data['loginname']);
				if($this->user_m->s_one($where_unique)){
					ajax_return(false,'已经存在登录名：'.$data['loginname']);
				}

				$data['pwd']=hc_pass($data['pwd']);
				$data['regtime']=time();
				$data['online']=0;
				if(($userid=$this->user_m->a_one($data))===false){
								addlog($this->pgcinfo,'失败','添加用户信息id:'.$userid);
								ajax_return(false,'添加失败');
				}else{
						addlog($this->pgcinfo,'成功','添加用户信息id:'.$userid);
						ajax_return(true,'添加成功');
				}
			}
			//查询角色分类
			$role_list=$this->role_m->s_all(array());
			$this->assign('roleList',$role_list);
			$this->display('user/add.tpl');
	}
	//编辑用户
	public function userEdit(){
			if(isset($_POST['ajaxs'])){
					$data=ajaxs_from($_POST['arr']);
					$where['id']=array('=',$data['id']);
					if(trim($data['pwd'])==''){
							unset($data['pwd']);
					}else{
						$data['pwd']=hc_pass($data['pwd']);
					}
					if($this->user_m->e_one($where,$data)===false){
						addlog($this->pgcinfo,'失败','修改用户信息id:'.$data['id']);
						ajax_return(false,'修改失败');
					}else{
						addlog($this->pgcinfo,'成功','修改用户信息id:'.$data['id']);
						ajax_return(true,'修改成功');
					}
			}else if(isset($_GET['id'])){
					$where['id']=array('=',$_GET['id']);
					$list=$this->user_m->s_one($where);
					if(!$list){
						echo js::alert('不存在用户信息');exit;
					}
					$this->assign('list',$list);
					//查询角色分类
					$role_list=$this->role_m->s_all(array());
					$this->assign('roleList',$role_list);
					$this->display('user/edit.tpl');
			}
	}
	public function userPass(){
			if(isset($_POST['ajaxs'])){
				$data=ajaxs_from($_POST['arr']);
				$oldpwd=hc_pass($data['oldpwd']);
				$newpwd=$data['newpwd'];
				$pwd=$data['pwd'];
				if($newpwd==''){
						ajax_return(false,'新密码不能为空');
				}elseif($newpwd!=$pwd){
						ajax_return(false,'两次密码不同');
				}elseif($oldpwd!=$this->pgcinfo['pwd']){
						ajax_return(false,'原密码出错');
				}
				$where['id']=array('=',$this->pgcinfo['id']);
				$datanew['pwd']=hc_pass($pwd);
				if($this->user_m->e_one($where,$datanew)===false){
					addlog($this->pgcinfo,'失败','修改用户密码id:'.$this->pgcinfo['id']);
					ajax_return(false,'修改失败');
				}else{
					addlog($this->pgcinfo,'成功','修改用户密码id:'.$this->pgcinfo['id']);
					ajax_return(true,'修改成功');
				}
			}

			$this->assign('loginname',$this->pgcinfo['loginname']);
			$this->display('user/pass.tpl');
	}
	//删除用户
	public function userDel(){
		if(isset($_POST['id'])){
				if($_POST['id']==1){
						ajax_return(false,'超级管理员无法删除');
				}
				$where['id']=array('=',$_POST['id']);
				if($this->user_m->d_one($where)===false){
						addlog($this->pgcinfo,'失败','删除用户id:'.$_POST['id']);
						ajax_return(false,'删除失败');
				}else{
						addlog($this->pgcinfo,'成功','删除用户id:'.$_POST['id']);
						ajax_return(true,'删除成功');
				}
		}
	}

}
?>
